package com.ybc.chinese.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.ybc.chinese.bean.CarouselFigure;
import com.ybc.chinese.bean.Home;
import com.ybc.chinese.view.HomeItemView;
import com.ybc.chinese.view.HomeTopView;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>首页<p/>
 * Created by Yubch on 17/2/16.
 */
public class HomeAdapter extends BaseAdapter {
    private Context mCtx;
    private List<CarouselFigure> mCaroueslFigures;
    private List<Home> mHomes;
    private HomeTopView homeTopView;

    public HomeAdapter(Context ctx) {
        mCtx = ctx;
        mCaroueslFigures = new ArrayList<>();
        mHomes = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return mHomes.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return mHomes.get(i - 1);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(position == 0) {
            if(convertView instanceof HomeTopView) {
                homeTopView = (HomeTopView) convertView;
            } else {
                homeTopView = new HomeTopView(mCtx);
            }
            homeTopView.setData(mCaroueslFigures);
            return homeTopView;
        } else {
            HomeItemView homeItemView;
            if(convertView instanceof HomeItemView) {
                homeItemView = (HomeItemView) convertView;
            } else {
                homeItemView = new HomeItemView(mCtx);
            }
            Home home = (Home) getItem(position);
            homeItemView.setData(home, position);
            return homeItemView;
        }
    }

    public void setData(List<Home> homes, boolean isMore) {
        if(!isMore && getCount() > 0) {
            mHomes.clear();
        }
        mHomes.addAll(homes);
        notifyDataSetChanged();
    }

    public void setCaroueslFigures(List<CarouselFigure> caroueslFigures) {
        if(mCaroueslFigures.size() > 0) {
            mCaroueslFigures.clear();
        }
        mCaroueslFigures.addAll(caroueslFigures);
        notifyDataSetChanged();
    }

    public HomeTopView getHomeTopView() {
        return homeTopView;
    }
}
